package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.entity.WithdrawalsRecordingEntity;
import io.renren.modules.app.entity.vo.QueryOrderParam;
import io.renren.modules.app.entity.vo.SelectOrderData;
import io.renren.modules.app.entity.vo.SelectWithdrawalsData;
import io.renren.modules.app.service.UserService;
import io.renren.modules.app.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.app.dao.OrdersDao;
import io.renren.modules.app.entity.OrdersEntity;
import io.renren.modules.app.service.OrdersService;
import org.springframework.transaction.annotation.Transactional;


@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, OrdersEntity> implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private UserService userService;


    /**
     * 分页获取订单
     *
     * @param order 订单信息类
     * @return
     */
    @Override
    public SelectOrderData queryPage(QueryOrderParam order) {
        SelectOrderData data = new SelectOrderData();
        QueryWrapper<OrdersEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(order.getOrderNo())) {
            wrapper.eq("order_no", order.getOrderNo());
        }
        wrapper.orderByDesc("create_time");
        Page<OrdersEntity> iPage = new Page<>(order.getPageNum(), order.getPageSize());
        IPage<OrdersEntity> cookBookIPage = baseMapper.selectPage(iPage, wrapper);
        List<OrdersEntity> list = cookBookIPage.getRecords();
        for (OrdersEntity ordersEntity : list) {
            UserEntity userEntity = new UserEntity();
            userEntity.setUserId(ordersEntity.getUserId());
            UserEntity user = userService.queryUserDetails(userEntity);
            ordersEntity.setUserName(user.getUsername());
        }

        data.setTotal(cookBookIPage.getTotal());
        data.setCurrentIndex(cookBookIPage.getCurrent());
        data.setSize(cookBookIPage.getSize());
        data.setPageCount(cookBookIPage.getPages());
        data.setOrdersList(list);
        return data;
    }

    /**
     * 统计待返金额
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public BigDecimal statisticsAmountToBeRefunded(Long userId) {
        return ordersDao.statisticsAmountToBeRefunded(userId);
    }

    /**
     * 新增订单
     *
     * @param order 订单信息类
     * @return
     */
    @Transactional
    @Override
    public Integer addOrder(OrdersEntity order) {
        return baseMapper.insert(order);
    }

    /**
     * 统计今日完成订单数
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Integer getTodayFinishOrder(Long userId) {
        return ordersDao.getTodayFinishOrder(userId);
    }

    /**
     * 分页获取订单记录
     */
    @Override
    public SelectOrderData queryOrderList(QueryOrderParam order) {
        SelectOrderData data = new SelectOrderData();
        QueryWrapper<OrdersEntity> wrapper = new QueryWrapper<>();
        if (order.getUserId() != null) {
            wrapper.eq("user_id", order.getUserId());
        }
        if (order.getCount().equals(0)) {
            wrapper.eq("deal_with_status", 0);
            wrapper.orderByDesc("create_time");
            Page<OrdersEntity> iPage = new Page<>(order.getPageNum(), order.getPageSize());
            IPage<OrdersEntity> cookBookIPage = baseMapper.selectPage(iPage, wrapper);
            List<OrdersEntity> list = cookBookIPage.getRecords();

            data.setTotal(cookBookIPage.getTotal());
            data.setCurrentIndex(cookBookIPage.getCurrent());
            data.setSize(cookBookIPage.getSize());
            data.setPageCount(cookBookIPage.getPages());
            data.setOrdersList(list);
            return data;
        } else if (order.getCount().equals(1)) {
            wrapper.eq("deal_with_status", 1);
            wrapper.orderByDesc("create_time");
            Page<OrdersEntity> iPage = new Page<>(order.getPageNum(), order.getPageSize());
            IPage<OrdersEntity> cookBookIPage = baseMapper.selectPage(iPage, wrapper);
            List<OrdersEntity> list = cookBookIPage.getRecords();

            data.setTotal(cookBookIPage.getTotal());
            data.setCurrentIndex(cookBookIPage.getCurrent());
            data.setSize(cookBookIPage.getSize());
            data.setPageCount(cookBookIPage.getPages());
            data.setOrdersList(list);
            return data;
        } else if (order.getCount().equals(2)) {
            wrapper.eq("deal_with_status", 2);
            wrapper.orderByDesc("create_time");
            Page<OrdersEntity> iPage = new Page<>(order.getPageNum(), order.getPageSize());
            IPage<OrdersEntity> cookBookIPage = baseMapper.selectPage(iPage, wrapper);
            List<OrdersEntity> list = cookBookIPage.getRecords();

            data.setTotal(cookBookIPage.getTotal());
            data.setCurrentIndex(cookBookIPage.getCurrent());
            data.setSize(cookBookIPage.getSize());
            data.setPageCount(cookBookIPage.getPages());
            data.setOrdersList(list);
            return data;
        } else {
            return data;
        }
    }


    /**
     * 获取订单详情
     *
     * @param ordersEntity 订单信息类
     * @return 订单详情
     */
    @Override
    public OrdersEntity queryOrderDetails(OrdersEntity ordersEntity) {
        QueryWrapper<OrdersEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotNull(ordersEntity.getUserId())) {
            wrapper.eq("user_id", ordersEntity.getUserId());
        }
        if (StringUtils.isNotNull(ordersEntity.getOrderNo())) {
            wrapper.eq("order_no", ordersEntity.getOrderNo());
        }
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 修改订单信息
     *
     * @param orders 订单信息类
     * @return
     */
    @Transactional
    @Override
    public Integer editOrderId(OrdersEntity orders) {
        UpdateWrapper<OrdersEntity> wrapper = new UpdateWrapper<>();
        if (StringUtils.isNotNull(orders.getUserId())) {
            wrapper.eq("user_id", orders.getUserId());
        }
        if (StringUtils.isNotNull(orders.getOrderNo())) {
            wrapper.eq("order_no", orders.getOrderNo());
        }
        return baseMapper.update(orders, wrapper);
    }

}